package com.hyjx.framework.component.operatelog;

import com.hyjx.framework.common.Constants;
import com.hyjx.framework.service.SessionManager;
import com.hyjx.framework.uc.user.UserInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 操作日志拦截器
 */
public class OperLogInterceptor implements HandlerInterceptor {
    Logger log = LoggerFactory.getLogger(OperLogInterceptor.class);

    @Override
    public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3)
            throws Exception {

    }

    @Override
    public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3)
            throws Exception {

    }

    /**
     * 拦截前处理
     *
     * @param request
     * @param response
     * @param obj
     * @return
     *
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object obj) throws Exception {
        String uri = request.getRequestURI();
        Object ui = SessionManager.get(Constants.USERINFO);

        String user_id = "";
        if (ui instanceof UserInfo) {
            user_id = ((UserInfo) ui).getUser_account();
        }
        if (ui instanceof com.hyjx.framework.uc.admin.AdminModel) {
            user_id = ((com.hyjx.framework.uc.admin.AdminModel) ui).getAdmin_account();
        }
        if (ui != null) {
            OperatelogStatus.user_id = user_id;
            OperatelogStatus.url = uri;
            OperatelogStatus.varCon += 1;
        }
        return true;
    }
}